package mutilthread;

/**
 * Created with IntelliJ IDEA.
 * Description: 死锁
 * User: Li_yizYa
 * Date: 2025—03—24
 * Time: 16:38
 */
public class Demo7 {
    private static Object locker1 = new Object();
    private static Object locker2 = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
           synchronized (locker1) {
               System.out.println("t1 加锁 locker1 完成");
               //这里的 sleep 是为了确保，t1 和 t2 都分别先拿到 locker1 和 locker2 然后分别再拿对方的锁
               //如果没有 sleep 执行顺序就不可控，可能出现某个线程一口气拿两把锁
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               synchronized (locker2) {
                   System.out.println("t1 加锁 locker2 完成");
               }
           }
        });

        Thread t2 = new Thread(() -> {
           synchronized (locker2) {
               System.out.println("t2 加锁 locker2 完成");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               synchronized (locker1) {
                   System.out.println("t2 加锁 locker1 完成");
               }
           }
        });

        t1.start();
        t2.start();
    }
}
